package com.interview.seckilladvance.config;

import org.apache.catalina.connector.Connector;
import org.apache.coyote.http11.Http11NioProtocol;
import org.springframework.boot.web.embedded.tomcat.TomcatConnectorCustomizer;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.ConfigurableWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.stereotype.Component;

/**
 * @Function: 当Spring容器没有TomcatEmbeddedServletContainerFactory 这个bean, 会把此bean加载进来
 * @Author: aqie
 * @Date: 2019/8/14 15:19
 */
@Component
public class WebServerConfiguration implements WebServerFactoryCustomizer<ConfigurableWebServerFactory> {
    /**
     * 定制化tomcat 配置
     * @param factory 可配置webServer 工厂
     */
    @Override
    public void customize(ConfigurableWebServerFactory factory) {
        ((TomcatServletWebServerFactory)factory).addConnectorCustomizers(new TomcatConnectorCustomizer() {
            @Override
            public void customize(Connector connector) {
                Http11NioProtocol protocol = (Http11NioProtocol) connector.getProtocolHandler();
                // 定制 keepalive 30s
                protocol.setKeepAliveTimeout(30000);
                // 客户端发送超过一万个请求 断开连接
                protocol.setMaxKeepAliveRequests(10000);
            }
        });
    }
}
